package com.hbzhit.modules.nm.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hbzhit.common.extension.BaseServiceImpl;
import com.hbzhit.common.utils.Constant;
import com.hbzhit.common.utils.PageUtils;
import com.hbzhit.common.utils.ShiroUtils;
import com.hbzhit.modules.nm.dao.NoticeDao;
import com.hbzhit.modules.nm.entity.NoticeEntity;
import com.hbzhit.modules.nm.entity.NoticeStatEntity;
import com.hbzhit.modules.nm.form.NoticeQueryForm;
import com.hbzhit.modules.nm.service.NoticeService;
import com.hbzhit.modules.nm.service.NoticeStatService;
import com.hbzhit.modules.sys.entity.SysUserEntity;
import lombok.AllArgsConstructor;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 消息通知
 *
 * @author Tiger.Zhang
 * 2020-01-09 11:12
 **/
@Service("noticeService")
@AllArgsConstructor
public class NoticeServiceImpl extends BaseServiceImpl<NoticeDao, NoticeEntity> implements NoticeService {

    private final NoticeDao noticeDao;
    private final NoticeStatService noticeStatService;

    @Override
    public PageUtils<NoticeEntity> queryPage(NoticeQueryForm params) {
        SysUserEntity user = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        params.setUserId(user.getId());

        IPage<NoticeEntity> page = params.pageObj();
        List<NoticeEntity> list = noticeDao.selectNoticePage(page, params);
        page.setRecords(list);
        return new PageUtils<>(page);
    }

    @Override
    public void readNotice(Collection<Long> idList, Long userId) {
        idList.forEach(id -> {
            NoticeStatEntity ms = noticeStatService.getOne(new LambdaQueryWrapper<NoticeStatEntity>()
                    .eq(NoticeStatEntity::getNoticeId, id)
                    .eq(NoticeStatEntity::getUserId, userId)
                    , false
            );
            if (ms == null) {
                ms = new NoticeStatEntity();
                ms.setNoticeId(id);
                ms.setUserId(userId);
                ms.setIsRead(Constant.INT_Y);
                ms.setReadTime(DateTime.now());
                noticeStatService.save(ms);
            } else {
                ms.setIsRead(Constant.INT_Y);
                ms.setReadTime(DateTime.now());
                noticeStatService.updateById(ms);
            }
        });
    }

    @Override
    public void readNotice(Collection<Long> idList) {
        SysUserEntity user = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        readNotice(idList, user.getId());
    }

    @Override
    public void delNotice(Collection<Long> idList) {
        SysUserEntity user = ShiroUtils.getUser();

        idList.forEach(id -> {
            NoticeStatEntity ms = noticeStatService.getOne(new LambdaQueryWrapper<NoticeStatEntity>()
                            .eq(NoticeStatEntity::getNoticeId, id)
                            .eq(NoticeStatEntity::getUserId, user.getId())
                    , false
            );
            if (ms == null) {
                ms = new NoticeStatEntity();
                ms.setNoticeId(id);
                ms.setUserId(user.getId());
                ms.setIsDel(Constant.INT_Y);
                ms.setDeleteTime(DateTime.now());
                noticeStatService.save(ms);
            } else {
                ms.setIsDel(Constant.INT_Y);
                ms.setDeleteTime(DateTime.now());
                noticeStatService.updateById(ms);
            }
        });
    }

    /**
     * 创建通知数据
     * @param category  类别
     * @param title     标题
     * @param content   内容
     * @param route     前端路由
     * @param targetUserIds 目标用户ID集合
     * @param senderId  发送人ID
     * @param source    来源
     */
    @Override
    public void saveNotice(Integer category, String title, String content, String route, Collection<Long> targetUserIds, Long senderId, String source) {
        List<NoticeEntity> msgList = new ArrayList<>();
        for (Long id: targetUserIds) {
            NoticeEntity msg = new NoticeEntity();
            msg.setCategory(category);
            msg.setTargetUserId(id);
            msg.setTitle(title);
            msg.setContent(content);
            msg.setRoute(route);
            msg.setSource(source);
            msg.setCreator(senderId);
            msg.setCategory(category);
            msgList.add(msg);
        }
        saveBatch(msgList);
    }
}
